Ti.include('functions.js');
var win1 = Ti.UI.currentWindow;
var probe = win1.probe;

var xhrXMLCurrentTemp = Titanium.Network.createHTTPClient();
var xhrXMLOn = Titanium.Network.createHTTPClient();

var xhrXMLGetTemp = Titanium.Network.createHTTPClient();
var xhrXMLGetVariation = Titanium.Network.createHTTPClient();
var xhrXMLGetName = Titanium.Network.createHTTPClient();
var xhrXMLSaveChanges = Titanium.Network.createHTTPClient();
var xhrXMLPrintLabel = Titanium.Network.createHTTPClient();
var xhrXMLGetOgUrl = Titanium.Network.createHTTPClient();

var view = Titanium.UI.createView({
	height: WindowHeight,
	width: WindowWidth,
	top: 0
});

xhrXMLGetOgUrl.onload = function() {
	if (this.status == 200) {
		var og = "";
		Ti.API.info(this.responseText);
		var xml = Ti.XML.parseString(this.responseText);
		//This.API.info(xml);
		var xml_doc = xml.documentElement.getElementsByTagName("result");
		for (var i=0;i<xml_doc.length;i++) {
			og = xml_doc.item(i).getElementsByTagName("og").item(0).text;
		}
		txtOG.value = og;	
	}
};

xhrXMLPrintLabel.onload = function() {
	if (this.status == 200) {
		var message = "";
		//Ti.API.info(this.responseText);
		var xml = Ti.XML.parseString(this.responseText);
		//This.API.info(xml);
		var xml_doc = xml.documentElement.getElementsByTagName("result");
		for (var i=0;i<xml_doc.length;i++) {
			message = xml_doc.item(i).getElementsByTagName("message").item(0).text;
		}
		alert("Print Job Sent");
		
		
	}
};

xhrXMLSaveChanges.onload = function() {
	if (this.status == 200) {
		var status = "";
		//Ti.API.info(this.responseText);
		var xml = Ti.XML.parseString(this.responseText);
		//This.API.info(xml);
		var xml_doc = xml.documentElement.getElementsByTagName("result");
		for (var i=0;i<xml_doc.length;i++) {
			status = xml_doc.item(i).getElementsByTagName("status").item(0).text;
		}
		Ti.API.info(status);
		if (status == "Success") {
			alert("Changes Saved");	
		} else {
			alert("Save Failed!");
		}
		
		
	}
};

xhrXMLGetName.onload = function() {
	if (this.status == 200) {
		var name = -1;
		//Ti.API.info(this.responseText);
		var xml = Ti.XML.parseString(this.responseText);
		//This.API.info(xml);
		var xml_doc = xml.documentElement.getElementsByTagName("result");
		for (var i=0;i<xml_doc.length;i++) {
			name = xml_doc.item(i).getElementsByTagName("name").item(0).text;
		}
		txtName.value = name;
	}
};

xhrXMLGetVariation.onload = function() {
	if (this.status == 200) {
		var variation = -1;
		//Ti.API.info(this.responseText);
		var xml = Ti.XML.parseString(this.responseText);
		//This.API.info(xml);
		var xml_doc = xml.documentElement.getElementsByTagName("result");
		for (var i=0;i<xml_doc.length;i++) {
			variation = xml_doc.item(i).getElementsByTagName("variance").item(0).text;
		}
		txtVariation.value = variation;
	}
};

xhrXMLGetTemp.onload = function() {
	if (this.status == 200) {
		var temp = -1;
		//Ti.API.info(this.responseText);
		var xml = Ti.XML.parseString(this.responseText);
		//This.API.info(xml);
		var xml_doc = xml.documentElement.getElementsByTagName("result");
		for (var i=0;i<xml_doc.length;i++) {
			temp = xml_doc.item(i).getElementsByTagName("temp").item(0).text;
		}
		txtTemp.value = temp;
	}
};


xhrXMLCurrentTemp.onload = function() {
	if (this.status == 200) {
		var temp = -1;
		//Ti.API.info(this.responseText);
		var xml = Ti.XML.parseString(this.responseText);
		//This.API.info(xml);
		var xml_doc = xml.documentElement.getElementsByTagName("result");
		for (var i=0;i<xml_doc.length;i++) {
			temp = xml_doc.item(i).getElementsByTagName("temp").item(0).text;
		}
		lblCurrentTemp.text = "Current Temp: " + temp;
	}
};

xhrXMLOn.onload = function() {
	if (this.status == 200) {
		var on = -1;
		//Ti.API.info(this.responseText);
		var xml = Ti.XML.parseString(this.responseText);
		//This.API.info(xml);
		var xml_doc = xml.documentElement.getElementsByTagName("result");
		for (var i=0;i<xml_doc.length;i++) {
			on = xml_doc.item(i).getElementsByTagName("on").item(0).text;
		}
		lblHeatOn.text = "Heat On: " + on;
	}
};

var onUrl = "http://"+Ti.App.beerwatch+"/api.php?command=getHeatOn&probe=" + probe;
xhrXMLOn.open("GET",onUrl,false);
xhrXMLOn.send();

var tempUrl = "http://"+Ti.App.beerwatch+"/api.php?command=getCurrentTemp&probe=" + probe;
xhrXMLCurrentTemp.open("GET",tempUrl,false);
xhrXMLCurrentTemp.send();


var lblCurrentTemp = Ti.UI.createLabel({
	Top: 10,
	left: 10
});

var lblHeatOn = Ti.UI.createLabel({
	Top: 30,
	left: 10
});

var lblSetTemp = Ti.UI.createLabel({
	Top: 50,
	left: 10,
	text: "Temp Set"
});


var txtTemp = Ti.UI.createTextField({
  borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
  top: 75, 
  left: 10,
  width: WindowWidth -20,
  autocapitalization: Titanium.UI.TEXT_AUTOCAPITALIZATION_NONE,
  keyboardType:Titanium.UI.KEYBOARD_PHONE_PAD
});


var lblSetVariation = Ti.UI.createLabel({
	Top: 105,
	left: 10,
	text: "Variation Set"
});


var txtVariation = Ti.UI.createTextField({
  borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
  top: 125, 
  left: 10,
  width: WindowWidth -20,
  autocapitalization: Titanium.UI.TEXT_AUTOCAPITALIZATION_NONE,
  keyboardType:Titanium.UI.KEYBOARD_PHONE_PAD
});

var lblSetName = Ti.UI.createLabel({
	Top: 155,
	left: 10,
	text: "Name"
});


var txtName = Ti.UI.createTextField({
  borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
  top: 175, 
  left: 10,
  width: WindowWidth -20,
  autocapitalization: Titanium.UI.TEXT_AUTOCAPITALIZATION_NONE
});

var lblPrintLabel = Ti.UI.createLabel({
	Top: 205,
	left: 10,
	text: "Print Label"
});

var lblOG = Ti.UI.createLabel({
	Top: 225,
	left: 10,
	text: "OG"
});

var txtOG = Ti.UI.createTextField({
  borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
  top: 245, 
  left: 10,
  width: WindowWidth -20,
  autocapitalization: Titanium.UI.TEXT_AUTOCAPITALIZATION_NONE
});

var lblDate = Ti.UI.createLabel({
	Top: 275,
	left: 10,
	text: "Date"
});
var d = js_yyyy_mm_dd();
var txtDate = Ti.UI.createTextField({
  borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
  top: 295, 
  left: 10,
  width: WindowWidth -20,
  autocapitalization: Titanium.UI.TEXT_AUTOCAPITALIZATION_NONE,
  value: d
});

var lblFg = Ti.UI.createLabel({
	Top: 325,
	left: 10,
	text: "FG"
});
var txtFg = Ti.UI.createTextField({
  borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
  top: 345, 
  left: 10,
  width: WindowWidth -20,
  autocapitalization: Titanium.UI.TEXT_AUTOCAPITALIZATION_NONE
});


var btnPrint = Ti.UI.createButton({
	title: 'Print Label',
	top: 385,
	left: 10,
	width: WindowWidth -20
});

var SettempUrl =  "http://"+Ti.App.beerwatch+"/api.php?command=getSetTemp&probe=" + probe;
xhrXMLGetTemp.open("GET",SettempUrl,false);
xhrXMLGetTemp.send();

var GetOgUrl =  "http://"+Ti.App.beerwatch+"/api.php?command=getOG&probe=" + probe;
Ti.API.info(GetOgUrl);
xhrXMLGetOgUrl.open("GET",GetOgUrl,false);
xhrXMLGetOgUrl.send();

var Settempvariation =  "http://"+Ti.App.beerwatch+"/api.php?command=getTempVariation&probe=" + probe;
xhrXMLGetVariation.open("GET",Settempvariation,false);
xhrXMLGetVariation.send();

var theName =  "http://"+Ti.App.beerwatch+"/api.php?command=getName&probe=" + probe;
xhrXMLGetName.open("GET",theName,false);
xhrXMLGetName.send();

var interval = setInterval(function(){
	var onUrl = "http://"+Ti.App.beerwatch+"/api.php?command=getHeatOn&probe=" + probe;
	xhrXMLOn.open("GET",onUrl,false);
	xhrXMLOn.send();

	var tempUrl = "http://"+Ti.App.beerwatch+"/api.php?command=getCurrentTemp&probe=" + probe;
	xhrXMLCurrentTemp.open("GET",tempUrl,false);
	xhrXMLCurrentTemp.send();
},2000);

txtTemp.addEventListener('focus',function(e) {
	view.top = -40;
});

txtVariation.addEventListener('focus',function(e) {
	view.top = -50;
});
txtName.addEventListener('focus',function(e) {
	view.top = -60;
});

txtTemp.addEventListener('blur', function(e) {
	view.top = 0;
});
txtFg.addEventListener('focus',function(e) {
	view.top = -150;
});

txtFg.addEventListener('blur', function(e) {
	view.top = 0;
});

txtOG.addEventListener('focus',function(e) {
	view.top = -135;
});

txtOG.addEventListener('blur', function(e) {
	view.top = 0;
});

txtDate.addEventListener('focus',function(e) {
	view.top = -145;
});

txtDate.addEventListener('blur', function(e) {
	view.top = 0;
});


win1.addEventListener('click',function(e) {
	view.top = 0;
	txtTemp.blur();
	txtVariation.blur();
	txtName.blur();
	txtOG.blur
	txtDate.blur();
	txtName.blur();
});

btnSave= Titanium.UI.createButton({
	title: 'Save'
});


btnSave.addEventListener('click',function(e) {
	view.top = 0;
	txtTemp.blur();
	txtVariation.blur();
	txtName.blur();
	txtOG.blur
	txtDate.blur();
	txtName.blur();		
	var saveChanges = "http://"+Ti.App.beerwatch+"/api.php?command=saveChanges&probe=" + probe +"&temp=" + txtTemp.value + "&variation=" + txtVariation.value + "&name=" + txtName.value+ "&og=" + txtOG.value;
	xhrXMLSaveChanges.open("GET",saveChanges,false);
	xhrXMLSaveChanges.send();

});

btnPrint.addEventListener('click',function(e) {
	var printLabel = "http://"+Ti.App.beerwatch+"/api.php?command=makeLabel&date="+txtDate.value+"&name="+txtName.value+"&og="+txtOG.value+"&fg="+txtFg.value;
	xhrXMLPrintLabel.open("GET",printLabel,false);
	xhrXMLPrintLabel.send();
	view.top = 0;
	txtTemp.blur();
	txtVariation.blur();
	txtName.blur();
	txtOG.blur
	txtDate.blur();
	txtName.blur();
		
});

win1.rightNavButton = btnSave;

win1.add(view);
view.add(lblCurrentTemp);
view.add(lblHeatOn);
view.add(lblSetTemp);
view.add(txtTemp);
view.add(lblSetVariation);
view.add(txtVariation);
view.add(lblSetName);
view.add(lblPrintLabel);
view.add(lblOG);
view.add(txtOG);
view.add(lblDate);
view.add(txtDate);
view.add(lblFg);
view.add(txtFg);
view.add(btnPrint);
view.add(txtName);
